import 'dart:convert';

import 'package:finance_app/src/common/config.dart';

import 'web_socket.dart';

class SocketRequest {
  ///回复心跳，当服务端返回base.ping时
  static void responsePong() {
    var dataMap = Map<String, dynamic>();
    dataMap["event"] = "pusher:ping";
    dataMap['data'] = {};
    // dataMap['ts'] = DateTime.now().millisecondsSinceEpoch;
    String data = json.encode(dataMap).toString();
    WebSocketProvide.sendMessage(data);
  }

  //重连后重新订阅对应的通道
  static Set<dynamic> _subList = {};

  static void reStartSub() {
    if (_subList.isNotEmpty) {
      List<dynamic> willSubList = List.from(_subList);
      for (final item in willSubList) {
        WebSocketProvide.sendMessage(item);
      }
    }
  }

  static void subUserChat({String auth = "", String channel}) {
    var dataMap = Map<String, dynamic>();
    dataMap["event"] = "pusher:subscribe";
    dataMap['data'] = {
      'auth': auth,
      'channel': channel,
    };
    // dataMap['ts'] = DateTime.now().millisecondsSinceEpoch;
    String data = json.encode(dataMap).toString();
    _subList.add(data);
    WebSocketProvide.sendMessage(data);
  }
}
